<?php

namespace Console\Controller;

use Console\Logic;
use Console\Common\Library\Page;

/**
 * 收徒配置controller
 * Class RecommController
 * @package Console\Controller
 */
class RecommController extends CstBaseController
{
    private $recommLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->recommLogic = A('Console/Recomm', 'Logic');
    }

    /**
     * 编辑分成配置
     */
    public function editConf()
    {
        $infoRes = $this->recommLogic->getConfInfo();
        $info = $infoRes['Result'];
        $this->assign("info", $info);
        $this->display();
    }

    /**
     * 提交编辑分成配置
     */
    public function doEditConf()
    {
        $data = I('post.');
        $id = $data['id'];
        unset($data['id']);

        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("系统异常"));
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $res = $this->recommLogic->editConf($id, $userId, $data);
        $this->ajaxReturn($res);
    }

    /**
     * 查询推手徒弟裂变任务
     *
     * @return void
     */
    public function taskList()
    {
        $this->display();
    }

    /**
     * 获取徒弟任务列表
     *
     * @return void
     */
    public function doGetTaskList()
    {
        $phone = I('post.phone');
        $finishTime = I('post.finish_time');
        $page = I('post.page');
        $page = $page?$page:1;
        $pageSize = 50;

        if (empty($phone) || empty($finishTime)) {
            $this->ajaxReturn(static::$res->err("参数异常"));
        }

        $userLogic = new Logic\UserLogic();
        $userInfoRes = $userLogic->getInfo(array(
            "phone"=>$phone,
            "user_state"=>1
        ));
        $userInfo = $userInfoRes["Result"];
        if (empty($userInfo)) {
            $this->ajaxReturn(static::$res->err("手机号未找到用户"));
        }

        $userId = $userInfo['id'];
        $taskListRes = $this->recommLogic->getRecommTaskList($userId, $finishTime, $page, $pageSize);
        if (static::$res->isErr($taskListRes)) {
            $this->ajaxReturn($taskListRes);
        }

        $data = $taskListRes["Result"];
        $list = $data["list"];
        $total = $data["count"];
        $incomes = $data["incomes"];
        if (empty($total)) {
            $this->ajaxReturn(static::$res->err("徒弟未进行任何任务"));
        }
        if (empty($list)) {
            $this->ajaxReturn(static::$res->err("暂无数据"));
        }

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $taskLogic = new Logic\TaskLogic();
        $typeDic = $taskLogic->getTypeDic();
        $typeDic[10] = '问卷赚';

        $this->assign("typeDic", $typeDic);
        $this->assign("pageTotal", $pageTotal);
        $this->assign("pageHtml", $pageHtml);
        $this->assign("list", $list);
        $this->assign("total", $total);
        $this->assign("incomes", $incomes);
        $html = $this->fetch('doGetTaskList');

        $res = array(
            "list" => $list,
            "html" => $html,
            "total" => $total,
            "pageTotal" => $pageTotal
        );
        $this->ajaxReturn(static::$res->suc("成功", $res));

    }
}
